An Extended Example
We will start with a new movie (File > New > Movie or Ctrl+n). Every movie needs a cast. Our movie will consist of a 3D world and its physical representation.
- Add a blank Havok Physics Scene member by using the Insert menu: Insert > Media Element > Havok Physics Scene.
- Add a blank Shockwave 3D member using Insert > Media Element > Shockwave 3D.
- Drag the Shockwave 3D member onto the stage to create a sprite.
Currently our 3D world is empty. We will now populate it with a few simple objects. This can be done via a simple Lingo script.
- Add a new behavior by selecting Window > Inspectors > Behavior on the main menu and then clicking the + icon followed by selecting New Behavior.
- Drag the new behavior onto the 3D sprite.
Add the following text to the newly create Lingo behavior. This script creates a simple scene made up from two boxes and a sphere.
property w, hk
on beginSprite me
hk = member( 1 )
w = member( 2 )
hk.initialize( w, 0.1, 1 )
createVisibleObjects()
createPhysicalObjects()end
on createVisibleObjects me
-- create box
mr = w.newModelResource("TheBoxRes", #box)
mr.width = 2
mr.height = 2
mr.length = 2
m = w.newModel("TheBox", mr)
m.transform.rotation = vector(0,5,0)
m.transform.position = vector(0,0,10)
-- create ball
mr = w.newModelResource("TheBallRes", #sphere)
mr.radius = 1
m = w.newModel("TheBall", mr)
m.transform.position = vector(0,0,5)
-- create ground
mr = w.newModelResource("TheGroundRes", #box)
mr.width = 15
mr.height = 10
mr.length = 1
m = w.newModel("TheGround", mr)
-- point camera
c = w.camera[1]
c.transform.position = vector(0,20,20)
c.pointat(m.transform.position + vector(0,0,5), vector(0,0,1))
c.hither = 1
c.yon = 1000
w.directionalPreset = #bottomLeftend
on createPhysicalObjects me
-- box
m = w.model("TheBox")
m.addModifier(#meshdeform)
rb = hk.makeMovableRigidBody(m.name, 50)
-- ball
m = w.model("TheBall")
m.addModifier(#meshdeform)
rb = hk.makeMovableRigidBody(m.name, 100)
rb.restitution = 1
rb.friction = 0
-- ground
m = w.model("TheGround")
m.addModifier(#meshdeform)
rb = hk.makeFixedRigidBody(m.name)end
The lines specific to the Havok Xtra are highlighted in bold. Full details of these functions and properties can be found in the Havok Xtra Lingo Reference. We will only briefly examine them here.
Each physically simulated scene must be linked to a visible 3D equivalent. To make this link the hk.initialize() function is passed a reference to the Shockwave 3D cast member representing the visible world.
Note: hk.initialize() must be called first before any other actions relating to our physical simulation can be preformed.
After initialization the script creates a number of 3D models that become visible in the scene. Once created it is possible to take these models and give them physical properties - make them into rigid bodies. As can be seen in the createPhysicalObjects function we are making "TheBox" and "TheBall" objects into movable rigid bodies. Movable rigid bodies have a mass and are free to move, bounce and spin. "TheGround" is created as a fixed rigid body that will never move and will never be effected by over objects hitting it.
At this point, we have created a simple physical 3D world. However, if the movie is played the view though our stage sprite still shows a static scene. This is because the physical simulation must be advanced upon each frame. The Havok Xtra refers to this as stepping and provides a Lingo function to perform this action.
on exitFrame me
hk.step()
end
on endSprite me
hk.shutdown()
w.resetWorld()end
Add the above lines to your behavior script. The call to the stepping function (hk.step) will be preformed at the end of each frame before the movie advances to the next one. This causes the simulation to calculate the new positions and orientations of each model. This means that they are ready to be drawn within the stage sprite at the start of the new frame. Furthermore, when the movie stops and before we reset our 3D world to its original blank slate, the physics simulation needs to be shutdown as well.